package com.kingint.web.admin.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.mbg.bas.entity.BasDepartment;
import com.kingint.mbg.group.Insert;
import com.kingint.mbg.group.Update;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.validateUtil.ValidList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.kingint.common.unitedresult.K;

import com.kingint.web.admin.base.service.BasIdentityService;
import com.kingint.mbg.bas.entity.BasIdentity;

import javax.annotation.Resource;


/**
 * @描述：员工身份表控制类
 * @作者: wxh
 * @日期: 2020-12-23
 */
@RestController
@RequestMapping("/basIdentity")
@Api(value = "BasIdentityController", description = "员工身份表")
@CrossOrigin
@Slf4j
public class BasIdentityController{

    @Resource
    private UmsAdminService adminService;

    @Autowired
    public BasIdentityService basIdentityService;


    @ApiOperation(value = "添加员工身份表")
    @PostMapping("/create")
    //@PreAuthorize("hasAuthority('basIdentity:create')")
    public Object create(@Validated(Insert.class) @RequestBody BasIdentity basIdentity){
        Boolean bool=basIdentityService.saves(basIdentity);
        if(bool){
            return K.ok().data(basIdentity.getId());
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "批量添加员工身份表")
    @PostMapping("/insertList")
    // @PreAuthorize("hasAuthority('basIdentity:insertList')")
    public Object create(@RequestBody @Validated(Insert.class)  ValidList<BasIdentity> basIdentity){
        Boolean bool=basIdentityService.saveOrUpdateBatch(basIdentity);
        if(bool){
            return K.ok();
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "更新员工身份表")
    @PutMapping("/update")
    //@PreAuthorize("hasAuthority('basIdentity:update')")
    public Object update(@Validated(Update.class) @RequestBody BasIdentity basIdentity){
        Boolean bool= basIdentityService.updateById(basIdentity);
        if(bool){
            return K.ok().data(basIdentity.getId());
        }else{
            return K.error();
        }
    }


    @ApiOperation(value = "删除员工身份表")
    @DeleteMapping("/delete/{id}")
    // @PreAuthorize("hasAuthority('basIdentity:delete')")
    public Object delete(@PathVariable("id") Integer id){
        BasIdentity basIdentity = new BasIdentity();
        basIdentity.setId(id);
        basIdentity.setStatus(1);//删除
        Boolean bool= basIdentityService.updateById(basIdentity);
        if(bool){
            return K.ok();
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "根据员工身份表名称分页获取员工身份表列表")
    @GetMapping("/list")
    //@PreAuthorize("hasAuthority('basIdentity:list')")
    public Object getList(BasIdentity basIdentity,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if(StringUtils.isEmpty(hotelId)){
            log.debug("当前账号没有关联对应的酒店");
            return K.error();
        }

        Page<BasIdentity> page = new Page<>(pageNum, pageSize);
        QueryWrapper<BasIdentity> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0)
                .eq("hotel_id",hotelId);
        String name = basIdentity.getName();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        IPage<BasIdentity> info=basIdentityService.page(page,wrapper);
        return K.ok().data(info);
    }

    @ApiOperation(value = "根据编号查询员工身份表信息")
    @GetMapping("/{id}")
    //@PreAuthorize("hasAuthority('basIdentity:get')")
    public Object getItem(@PathVariable("id") Long id){
        BasIdentity basIdentity=basIdentityService.getById(id);
        return K.ok().data(basIdentity);
    }
}